package com.sangmo.boot.auth.api;

import com.sangmo.boot.auth.core.AuthorizeService;
import com.sangmo.boot.auth.core.interfaces.GrantCodeRequest;
import com.sangmo.boot.auth.core.interfaces.GrantTokenRequest;
import com.sangmo.boot.auth.core.interfaces.ValidateTokenRequest;
import com.sangmo.boot.framework.api.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/authorize/v1")
public class AuthorizeApi {

	@Autowired
    private AuthorizeService authorizeService;

	@PostMapping("grant")
	public @ResponseBody
    Object grant(@RequestBody @Valid GrantTokenRequest req) {
		Object result = authorizeService.grant(req);
		return ApiResponse.make(result);
	}

	@PostMapping("validate")
	public @ResponseBody
    Object validate(@RequestBody @Valid ValidateTokenRequest req) {
		Object auth = authorizeService.validate(req);
		return ApiResponse.make(auth);
	}

	@PostMapping("code")
	public @ResponseBody
    Object gainCode(@RequestBody @Valid GrantCodeRequest req) {
		authorizeService.gainCode(req);
		return ApiResponse.make();
	}

	@PostMapping("logout")
	public @ResponseBody
    Object logout(@RequestParam("access_token") String token) {
		authorizeService.logout(token);
		return ApiResponse.make();
	}
}
